<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="85px">
<!--      <el-form-item label="机构名称" prop="orgname">-->
<!--        <el-input-->
<!--          v-model="queryParams.orgname"-->
<!--          placeholder="请输入机构名称"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="采购日期">
        <el-date-picker
          v-model="daterangeDates"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="供应商编号" prop="supplycode">
        <el-input
          v-model="queryParams.supplycode"
          placeholder="请输入供应商编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="供应商名称" prop="supplyname">
        <el-input
          v-model="queryParams.supplyname"
          placeholder="请输入供应商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="商品编号" prop="goodscode">
        <el-input
          v-model="queryParams.goodscode"
          placeholder="请输入商品编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="goodsname">
        <el-input
          v-model="queryParams.goodsname"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="商品规格" prop="goodsspec">-->
<!--        <el-input-->
<!--          v-model="queryParams.goodsspec"-->
<!--          placeholder="请输入商品规格"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="生产厂家" prop="manufacturer">-->
<!--        <el-input-->
<!--          v-model="queryParams.manufacturer"-->
<!--          placeholder="请输入生产厂家"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="单位" prop="unit">-->
<!--        <el-input-->
<!--          v-model="queryParams.unit"-->
<!--          placeholder="请输入单位"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="产地" prop="place">-->
<!--        <el-input-->
<!--          v-model="queryParams.place"-->
<!--          placeholder="请输入产地"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="数量" prop="num">-->
<!--        <el-input-->
<!--          v-model="queryParams.num"-->
<!--          placeholder="请输入数量"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="进价" prop="purtaxprice">-->
<!--        <el-input-->
<!--          v-model="queryParams.purtaxprice"-->
<!--          placeholder="请输入进价"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="商品批号" prop="batchcode">
        <el-input
          v-model="queryParams.batchcode"
          placeholder="请输入商品批号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="有效期至" prop="valdate">-->
<!--        <el-input-->
<!--          v-model="queryParams.valdate"-->
<!--          placeholder="请输入有效期至"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="货位名称" prop="locatname">-->
<!--        <el-input-->
<!--          v-model="queryParams.locatname"-->
<!--          placeholder="请输入货位名称"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['flow:kcjc:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['flow:kcjc:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['flow:kcjc:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['flow:kcjc:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

    <el-table v-loading="loading" :data="kcjcList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="ID" align="center" prop="id" />-->
<!--      <el-table-column label="机构名称" align="center" prop="orgname" />-->
      <el-table-column label="供应商编号" align="center" prop="supplycode" />
      <el-table-column label="供应商名称" align="center" prop="supplyname" />
      <el-table-column label="商品编号" align="center" prop="goodscode" />
      <el-table-column label="商品名称" align="center" prop="goodsname" />
      <el-table-column label="商品规格" align="center" prop="goodsspec" />
      <el-table-column label="生产厂家" align="center" prop="manufacturer" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="产地" align="center" prop="place" />
      <el-table-column label="数量" align="center" prop="num" />
      <el-table-column label="进价" align="center" prop="purtaxprice" />
      <el-table-column label="商品批号" align="center" prop="batchcode" />
      <el-table-column label="有效期至" align="center" prop="valdate" />
      <el-table-column label="货位名称" align="center" prop="locatname" />
      <el-table-column label="采购日期" align="center" prop="dates" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dates, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="药检报告" align="center" prop="piclink" />-->
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['flow:kcjc:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['flow:kcjc:remove']"-->
<!--          >删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品库存对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="企业名称" prop="entname">
          <el-input v-model="form.entname" placeholder="请输入企业名称" />
        </el-form-item>
        <el-form-item label="机构名称" prop="orgname">
          <el-input v-model="form.orgname" placeholder="请输入机构名称" />
        </el-form-item>
        <el-form-item label="供应商编号" prop="supplycode">
          <el-input v-model="form.supplycode" placeholder="请输入供应商编号" />
        </el-form-item>
        <el-form-item label="供应商名称" prop="supplyname">
          <el-input v-model="form.supplyname" placeholder="请输入供应商名称" />
        </el-form-item>
        <el-form-item label="采购日期" prop="dates">
          <el-date-picker clearable
            v-model="form.dates"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择采购日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="商品编号" prop="goodscode">
          <el-input v-model="form.goodscode" placeholder="请输入商品编号" />
        </el-form-item>
        <el-form-item label="商品名称" prop="goodsname">
          <el-input v-model="form.goodsname" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品规格" prop="goodsspec">
          <el-input v-model="form.goodsspec" placeholder="请输入商品规格" />
        </el-form-item>
        <el-form-item label="生产厂家" prop="manufacturer">
          <el-input v-model="form.manufacturer" placeholder="请输入生产厂家" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="产地" prop="place">
          <el-input v-model="form.place" placeholder="请输入产地" />
        </el-form-item>
        <el-form-item label="数量" prop="num">
          <el-input v-model="form.num" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="进价" prop="purtaxprice">
          <el-input v-model="form.purtaxprice" placeholder="请输入进价" />
        </el-form-item>
        <el-form-item label="商品批号" prop="batchcode">
          <el-input v-model="form.batchcode" placeholder="请输入商品批号" />
        </el-form-item>
        <el-form-item label="有效期至" prop="valdate">
          <el-input v-model="form.valdate" placeholder="请输入有效期至" />
        </el-form-item>
        <el-form-item label="货位名称" prop="locatname">
          <el-input v-model="form.locatname" placeholder="请输入货位名称" />
        </el-form-item>
        <el-form-item label="药检报告" prop="piclink">
          <el-input v-model="form.piclink" placeholder="请输入药检报告" />
        </el-form-item>
        <el-form-item label="商品助记码" prop="goodlogogram">
          <el-input v-model="form.goodlogogram" placeholder="请输入商品助记码" />
        </el-form-item>
        <el-form-item label="生产厂家助记码" prop="manulogogram">
          <el-input v-model="form.manulogogram" placeholder="请输入生产厂家助记码" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listKcjc, getKcjc, delKcjc, addKcjc, updateKcjc } from "@/api/flow/kcjc";

export default {
  name: "Kcjc",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品库存表格数据
      kcjcList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 生产厂家助记码时间范围
      daterangeDates: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orgname: null,
        supplycode: null,
        supplyname: null,
        dates: null,
        goodscode: null,
        goodsname: null,
        goodsspec: null,
        manufacturer: null,
        unit: null,
        place: null,
        num: null,
        purtaxprice: null,
        batchcode: null,
        valdate: null,
        locatname: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    // this.datatimerq()
    this.getList();
  },
  methods: {
    //日期计算-上个月1号至当前日
    datatimerq(){
      let dateTimes = new Date();//当前日期
      let nian=dateTimes.getFullYear()//当前年
      let yue=dateTimes.getMonth()+1//当前月
      let ri=dateTimes.getDate()//当前日
      if(yue<10){yue='0'+yue}
      if(ri<10){ri='0'+ri}
      this.daterangeDates=[nian+'-'+yue+'-'+'01',nian+'-'+yue+'-'+ri]
    },
    /** 查询商品库存列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeDates && '' != this.daterangeDates) {
        this.queryParams.params["beginDates"] = this.daterangeDates[0];
        this.queryParams.params["endDates"] = this.daterangeDates[1];
      }
      listKcjc(this.queryParams).then(response => {
        this.kcjcList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        entname: null,
        orgname: null,
        supplycode: null,
        supplyname: null,
        dates: null,
        goodscode: null,
        goodsname: null,
        goodsspec: null,
        manufacturer: null,
        unit: null,
        place: null,
        num: null,
        purtaxprice: null,
        batchcode: null,
        valdate: null,
        locatname: null,
        piclink: null,
        goodlogogram: null,
        manulogogram: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeDates = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品库存";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getKcjc(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品库存";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateKcjc(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addKcjc(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商品库存编号为"' + ids + '"的数据项？').then(function() {
        return delKcjc(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('flow/kcjc/export', {
        ...this.queryParams
      }, `kcjc_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
